let access_database = require("../database/access_database")
let adminSQLStore = require("../database/SQLSTORE_admin")
let missionSQLStore = require("../database/SQLSTORE_mission_collection")
let assignSQLStore = require("../database/SQLSTORE_mission_assign")
let staffSQLStore = require("../database/SQLSTORE_staff")
let message = require("./message")
let code_message = require("./JsonFileOprate")
module.exports = {
    "catAdminAccount": async function (req, res) {
        try {
            let results = await access_database(adminSQLStore.get("catAdmin"), [req.session.adminid])
            console.log(results)
            res.json(results)
        } catch (e) {
            console.log(e)
        }

    },
    "modifyAdminAccount": async function (req, res) {
        if (!req.body.name) {
            res.json(new message(450, null, code_message['450']))
        } else if (!req.body.name.startsWith("admin_")) {
            res.json(new message(452, null, code_message['452']))
        } else {
            try {
                let results = await access_database(adminSQLStore.get("updateAdmin"), [req.body.name, req.body.address, req.body.email, req.session.adminid])
                if (results.affectedRows > 0) {
                    res.json(new message(212, null, code_message['212']))
                } else {
                    res.json(new message(451, null, code_message['451']))
                }
            } catch (e) {
                console.log(e)
            }
        }


    },
    "appendMission": async function (req, res) {
        if (!(req.body.content && req.body.mission_name)) {
            //need true,null->false,now we hope,have null->true(!have null -> false),
            res.json(new message(453, null, code_message['453']))
        } else if (req.body.content.length > 200) {
            res.json(new message(454, null, code_message['454']))
        } else if (req.body.mission_name.length > 50) {
            res.json(new message(455, null, code_message['455']))
        } else if (req.body.mission_type.length > 20) {
            res.json(new message(456, null, code_message['456']))
        } else {
            try {
                let results = await access_database(missionSQLStore.get("insertMission"), [req.body.content, req.body.priority, req.body.mission_name, req.body.deadline, req.body.mission_type])

                if (results.affectedRows > 0) {
                    res.json(new message(213, results.insertId, code_message['213']))
                } else {
                    res.json(new message(457, null, code_message['457']))
                }
            } catch (e) {
                console.log(e)
            }
        }

    },
    "removeMission": async function (req, res) {
        if (!req.body.mission_id) {
            res.json(new message(453, null, code_message['453']))
        } else {
            try {
                let results = await access_database(missionSQLStore.get("deleteMission"), req.body.mission_id)
                if (results.affectedRows > 0) {
                    res.json(new message(214, null, code_message['214']))
                } else {
                    res.json(new message(459, null, code_message['459']))
                }
            } catch (e) {
                console.log(e)
            }
        }


    },
    cancelAssignedMission: async function (req, res) {
        if (!(req.body.staff_id && req.body.mission_id)) {
            res.json(new message(470, null, code_message['470']))
        } else {
            let results = await access_database(assignSQLStore.get("deleteMissionAssign"), [req.body.staff_id, req.body.mission_id])
            if (results.affectedRows > 0) {
                res.json(new message(233, null, code_message['233']))
            } else {

            }
        }
    },
    assignMission: async function (req, res) {
        if (!(req.body.staff_id && req.body.mission_id)) {
            res.json(new message(460, null, code_message['460']))
        } else {
            try {
                let results = await access_database(assignSQLStore.get("insertMissionAssign"), [req.body.staff_id, req.body.mission_id])
                if (results.affectedRows > 0) {
                    res.json(new message(217, null, code_message['217']))
                }
            } catch (e) {
                if (e.errno == 1062) {
                    console.log(e)
                    res.json(new message(461, null, code_message['461']))
                } else if (e.errno == 1452) {
                    res.json(new message(463, null, code_message['463']))
                }

            }

        }
    },
    selectTodayAllMission: async function (req, res) {
        try {
            let results = await access_database(missionSQLStore.get("selectTodayAllMission"))
            console.log(results)
            if (results.length > 0) {
                res.json(new message(220, results, code_message['220']))
            } else {
                res.json(new message(219, null, code_message['219']))
            }
        } catch (e) {
            console.log(e)
        }

    },
    selectTomorrowAllMission: async function (req, res) {
        try {
            let results = await access_database(missionSQLStore.get("selectTomorrowAllMission"))
            if (results.length > 0) {
                res.json(new message(222, results, code_message['222']))
            } else {
                res.json(new message(221, null, code_message['221']))
            }
        } catch (e) {
            console.log(e)
        }

    },
    selectAllStaff: async function (req, res) {
        try {
            let results = await access_database(staffSQLStore.get("CatAllStaff"))
            if (results.length > 0) {
                res.json(new message(229, results, code_message['229']))
            } else {
                res.json(new message(465, null, code_message['465']))
            }
        } catch (e) {
            console.log(e)
        }

    },
    selectAllMission: async function (req, res) {
        try {
            let results = await access_database(missionSQLStore.get("selectAllMission"))
            if (results.length > 0) {
                res.json(new message(229, results, code_message['229']))
            } else {
                res.json(new message(465, null, code_message['465']))
            }
        } catch (e) {
            console.log(e)
        }
    },
    selectAssignedMission: async function (req, res) {
        try {
            let results = await access_database(assignSQLStore.get("selectAssignMission"))
            if (results.length > 0) {
                res.json(new message(231, results, code_message['231']))
            } else {
                res.json(new message(468, null, code_message['468']))
            }
        } catch (e) {
            console.log(e)
        }
    },
    selectUnAssignedMission: async function (req, res) {
        try {
            let results = await access_database(assignSQLStore.get("selectUnAssignMission"))
            if (results.length > 0) {
                res.json(new message(232, results, code_message['232']))
            } else {
                res.json(new message(469, null, code_message['469']))
            }
        } catch (e) {
            console.log(e)
        }
    },


}